<?php 
	include("connect.php");
	$userID = $_POST["users_idNum"];
	$userID = intval($userID);
	$complaint = $_POST["complaints_complaint"];
	$ses_id = session_id();

	//Check for user in dataBase or create new entry
	$userExists = mysql_query("SELECT * FROM users WHERE idNum = '$userID'");
	$num_rows = mysql_num_rows($userExists);
	if ( $num_rows == 0 )
	{
		$names = array(0 => 'users', 1 => 'habits', 2 => 'background', 3 => 'moms', 4 => 'dads', 5 => 'siblings');
		for ($i = 0; $i<6; $i++)
		{
			$table = $names[$i];
			$sql_create_row = "INSERT INTO $table (idNum) VALUES ('$userID')";
			mysql_query($sql_create_row);
		}	
	}
	
	//Populate or update tables with data
	foreach($_POST as $key => $value)
	{
		if ((isset($_POST[$key])) and ($value!=NULL))
		{
			$pieces = explode("_", $key);
			$tableName = $pieces[0];
			$key = $pieces[1];
			if (($tableName != 'complaints') and ($key != 'idNum'))
			{
				if (is_array($value))
				{
					$value = implode(", ", $value);
				}
				$sql_update = "UPDATE $tableName SET $key='$value' WHERE idNum='$userID'";
				mysql_query($sql_update);
			}
		}
	}
?>